﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace eWallet.Security.Cryptography
{
	public sealed class SymmetricKeyCryptoModule : CryptoModule
	{

		public SymmetricKeyCryptoModule(SymmetricAlgorithm algorithm)
		{
			if (null == algorithm)
				throw new ArgumentNullException("algorithm");

			this.Algorithm = algorithm;
		}

		private SymmetricAlgorithm Algorithm;

		public override EncryptionMethod EncryptionMethod
		{
			get { return EncryptionMethod.SymmetricKey; }
		}



		public override byte[] Decrypt(byte[] cipher)
		{
			return this.Algorithm.CreateDecryptor().TransformFinalBlock(cipher, 0, cipher.Length);
		}

		public override byte[] Encrypt(byte[] data)
		{
			return this.Algorithm.CreateEncryptor().TransformFinalBlock(data, 0, data.Length);
		}

		public override string GetDescription()
		{
			List<byte> list = new List<byte>(this.Algorithm.IV);
			list.AddRange(this.Algorithm.Key);

			byte[] hash = SHA1CryptoServiceProvider.Create().ComputeHash(list.ToArray());

			return string.Format("{0} SHA1 {1}", this.EncryptionMethod, BitConverter.ToString(hash));
		}

	}
}
